package com.cn.ccp.action.course.online;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.HomeworkReportBean;
import com.cn.ccp.bean.OnlineExamBean;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.Contants;

public class HomeworkAction extends BaseAction {
	private Logger log = Logger.getLogger(HomeworkAction.class);
	private String courseName ,courseId,selectCourseId,settingId;
	private  List<OnlineExamBean> onlineExamList ;
	private  List<HomeworkReportBean> homeworkList;
	private SelectItem[] studentListItem ;
	
	private String studentSelectCourseTraing = "";//学生选课 题目信息
	private String selectStudentID ;
	
	private List<UserBean> studentList  ;
	private String type ;
	
	////
	private String studentTraingListTitle = "";
	private String studentTraingListTitleInfo = "";
	private int score;//该门课程学生考的分数
	/**
	 * 做练习题
	 * @return
	 */
	public String doTraining(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		courseId = varMap.get("courseId");
		selectCourseId = varMap.get("selectedCourseId");
		settingId = varMap.get("settingId");
		log.info("selectedCourseId = "+selectCourseId);
		log.info("courseId = "+courseId);
		log.info("settingId = "+settingId);
		onlineExamList = getOnlineExamService().getCourseResearch(settingId);
		return "dotraing";
	}

	public void charptsave(){
		for (int i = 0; i < onlineExamList.size(); i++) {
			OnlineExamBean onb = onlineExamList.get(i);
			if(onb.getSelectAnswer() != null && !onb.getSelectAnswer().equals("")){
				onb.setShowAnswerInfo(true);
				onlineExamList.set(i, onb);
			}
		}
		log.info("-----settingId = "+settingId);
		getCourseInvestigationService().commintInvestigation(settingId, onlineExamList);
	}
	
	/**
	 * 查看作业完成列表
	 * @return
	 */
	public String showHomeworkReportList(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		courseId = varMap.get("courseId");
		settingId = varMap.get("settingId");
		type = varMap.get("type");
		
//		if(courseId != null){
		studentList = getStudentService().getSelectCourseStudentList(courseId);
		
		for (int i = 0; i < studentList.size(); i++) {
			UserBean bean = studentList.get(i);
			onlineExamList = getOnlineExamService().getStudentSelectCourse(""+bean.getId(), settingId , type);
			if(onlineExamList.size() > 0)
				bean.setHiddenTringBtn(true);
			else
				bean.setHiddenTringBtn(false);
			
			studentList.set(i, bean);
				
		}
//			
//			studentListItem = new SelectItem[studentList.size()];
//			for (int i = 0; i < studentList.size(); i++) {
//				studentListItem[i] = new SelectItem(""+studentList.get(i).getId() ,studentList.get(i).getUserName());
//			}
//		}
//		studentSelectCourseTraing = "";
		
		log.info("-----courseId = "+courseId);
		//homeworkList = getCourseInvestigationService().getHomeworkReportList(courseId);
		return "homeworkList";
	}
	
	
	public String showStduetTraing(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		settingId = varMap.get("settingId");
		String studentid = varMap.get("userid");
		if(studentid == null || studentid.equals("") || studentid.equals("null")){
			UserBean userBean = CCPContants.getUserBean();
			studentid = String.valueOf(userBean.getId());
		}
		if(courseId == null){
			courseId = varMap.get("courseId");
		}
		type = varMap.get("type");
		log.info("-----courseId = "+courseId);
		log.info("-----settingId = "+settingId);
		log.info("-----studentid = "+studentid);
		onlineExamList = getOnlineExamService().getStudentSelectCourse(studentid, settingId , type);
		UserBean bean = getStudentService().findById(Integer.parseInt(studentid));
		studentTraingListTitleInfo = bean.getUserName();
		log.info("----------------type = "+type);
		if(type.equals(String.valueOf( Contants.COURSE_SETTING_TYPE_TEST))){//     试题
			studentTraingListTitle = BaseAction.getInstance().getMessages("course_page_course_test_info");
			score = getStudentService().getSelectedCourseByStuAndCourseId(studentid,courseId).getScore();
			log.info("----------------score = "+score);
		}else if(type.equals(String.valueOf( Contants.COURSE_SETTING_TYPE_HOMEWORK))){//作业
			studentTraingListTitle = BaseAction.getInstance().getMessages("course_page_course_training_info");
			studentTraingListTitleInfo = "";
		}
		
		return "studentTraningList";
	}
	
	
	public String getCourseName() {
		CourseBean bean = getCourseService().getBeanById(courseId);
		courseName = bean.getCourse_name();
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public List<OnlineExamBean> getOnlineExamList() {
		if(onlineExamList == null)
			onlineExamList = new ArrayList<OnlineExamBean>();
		log.info("------------------onlineExamList.size() = "+onlineExamList.size());
		return onlineExamList;
	}

	public void setOnlineExamList(List<OnlineExamBean> onlineExamList) {
		this.onlineExamList = onlineExamList;
	}

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public String getSelectCourseId() {
		return selectCourseId;
	}

	public void setSelectCourseId(String selectCourseId) {
		this.selectCourseId = selectCourseId;
	}

	public String getSettingId() {
		return settingId;
	}

	public void setSettingId(String settingId) {
		this.settingId = settingId;
	}

	public List<HomeworkReportBean> getHomeworkList() {
		return homeworkList;
	}

	public void setHomeworkList(List<HomeworkReportBean> homeworkList) {
		this.homeworkList = homeworkList;
	}
	public SelectItem[] getStudentListItem() {
		if(studentListItem == null)
			studentListItem = new SelectItem[0];
		return studentListItem;
	}

	public void setStudentListItem(SelectItem[] studentListItem) {
		this.studentListItem = studentListItem;
	}

	public String getStudentSelectCourseTraing() {
		return studentSelectCourseTraing;
	}

	public void setStudentSelectCourseTraing(String studentSelectCourseTraing) {
		this.studentSelectCourseTraing = studentSelectCourseTraing;
	}

	public String getSelectStudentID() {
		return selectStudentID;
	}

	public void setSelectStudentID(String selectStudentID) {
		this.selectStudentID = selectStudentID;
	}

	public List<UserBean> getStudentList() {
		if(studentList == null)
			studentList = new ArrayList<UserBean>();
		return studentList;
	}

	public void setStudentList(List<UserBean> studentList) {
		this.studentList = studentList;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getStudentTraingListTitle() {
		return studentTraingListTitle;
	}

	public void setStudentTraingListTitle(String studentTraingListTitle) {
		this.studentTraingListTitle = studentTraingListTitle;
	}

	public String getStudentTraingListTitleInfo() {
		return studentTraingListTitleInfo;
	}

	public void setStudentTraingListTitleInfo(String studentTraingListTitleInfo) {
		this.studentTraingListTitleInfo = studentTraingListTitleInfo;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}
	
	
}
